package com.suning.sawp.service.util;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.suning.redm.client.MailUtil;
import com.suning.sawp.constants.ExportConstants;
import com.suning.sawp.service.util.SCMConfigUtil;

@Component
public class EmailUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(EmailUtil.class);

    public static void sendEmail(String staffID, String fileUrl, String exportTypeName) {
        LOGGER.info("send Email, staffID:{}, fileUrl:{}, exportTypeName:{}", staffID, fileUrl, exportTypeName);
        String mailUrl = SCMConfigUtil.getConfig(ExportConstants.EmailSCMConfig.EMAIL_URL);// emailUrl;
        String wsUser = SCMConfigUtil.getConfig(ExportConstants.EmailSCMConfig.EMAIL_WS_USER);
        String wsPwd = SCMConfigUtil.getConfig(ExportConstants.EmailSCMConfig.EMAIL_WS_PWD);
        // 申请时候使用的appCode
        String appCode = ExportConstants.EmailSCMConfig.APP_CODE;
        String operation = ExportConstants.EmailSCMConfig.OPERATION;
        String serviceCode = ExportConstants.EmailSCMConfig.SERVICE_CODE;
        String uid = getUUID();
        // 自定义数据
        String data = emailData(fileUrl, exportTypeName);
        // 自定义数据分割符
        String dataSplit = "||";
        // 收件人地址(单个)
        String email = staffID + ExportConstants.EmailSCMConfig.EMAIL_SUFFIX;
        // 自定义发件人昵称
        String fromNickName = "店+系统";
        // 收件人昵称
        String nickName = staffID;
        // 模板Id
        Integer templateId = getEmailTempId(SCMConfigUtil.getConfig(ExportConstants.EmailSCMConfig.EMAIL_TEMPLATE_ID));
        // 是否需要Base64编码
        Boolean encode = false;
        // 优先级(1-3,1最低)
        Integer priorty = ExportConstants.EmailSCMConfig.PRIORTY_THREE;
        // 是否为测试邮件
        Boolean test = false;
        // 发送设置组Id
        Integer groupId = ExportConstants.EmailSCMConfig.GROUP_ID;

        try {
            boolean rsp = MailUtil.sendEmail(mailUrl, appCode, operation, serviceCode, uid, data, dataSplit, email,
                    fromNickName, nickName, templateId, wsPwd, wsUser, encode, priorty, test, groupId);
            LOGGER.info("send Email, rsp:" + rsp);
        } catch (Exception e) {
            LOGGER.error("send Email failed.", e);
        }
    }

    /**
     * 
     * 功能描述: 生成邮件内容<br>
     * 〈功能详细描述〉
     *
     * @param fileUrl
     * @param exportTypeName
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private static String emailData(String fileUrl, String exportTypeName) {
        StringBuilder sb = new StringBuilder();
        sb.append("您导出的\"");
        sb.append(exportTypeName);
        sb.append("\"报表已导出成功，请于12小时内点击下方链接进行下载 ，逾期失效。<br/><a title=\"下载\"  href=\"");
        sb.append(fileUrl);
        sb.append("\" target=\"_blank\" data-title=\"");
        sb.append(exportTypeName);
        sb.append("\">");
        sb.append(fileUrl);
        sb.append("</a>");
        return sb.toString();
    }

    private static String getUUID() {
        String s = UUID.randomUUID().toString() + UUID.randomUUID().toString();
        return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24, 44)
                + s.substring(45, 49) + s.substring(50, 54);
    }

    private static Integer getEmailTempId(String tempId) {
        Integer templateId = 0;
        try {
            templateId = Integer.parseInt(tempId);
        } catch (Exception e) {
            LOGGER.error("getEmailTempId failed.", e);
        }
        return templateId;
    }
}
